﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication4
{
    public partial class Demo01 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList2.Items.Add("福建");
                DropDownList2.Items.Add("广西");
                DropDownList2.Items.Add("海南");
            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string province = DropDownList2.SelectedValue;
            DropDownList3.Items.Clear();

            if (province=="福建")
            {
                DropDownList3.Items.Add("龙岩");
                DropDownList3.Items.Add("厦门");
                DropDownList3.Items.Add("福州");
            }
            else if (province == "广西")
            {
                DropDownList3.Items.Add("南宁");
                DropDownList3.Items.Add("崇左");
                DropDownList3.Items.Add("桂林");
            }
            else
            {
                DropDownList3.Items.Add("海口");
                DropDownList3.Items.Add("琼海");
                DropDownList3.Items.Add("五指山");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string hobby = null;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                //判断该选项是否被选中
                if (CheckBoxList1.Items[i].Selected)
                {
                    //将被选中的选项添加到变量中
                    hobby += CheckBoxList1.Items[i] + " ";
                }
            }
            string sex = null;
            foreach (ListItem item in DropDownList1.Items)
            {
                if (item.Selected)
                {
                    sex += item.Text + " " + item.Value;
                }
            }
            result.Text = $"用户名：{TextBox1.Text}<br />性别：{sex}<br />爱好：{hobby}<br />籍贯：{DropDownList2.SelectedValue}{DropDownList3.SelectedValue}<br />其他信息：{TextBox3.Text}";

            if (ful.HasFile)
            {
                //获取文件名
                string fileName = ful.FileName;
                //判断文件是否为图片
                if (fileName.EndsWith(".jpg") || fileName.EndsWith(".png") || fileName.EndsWith(".jpeg"))
                {
                    //获取文件的绝对路径
                    string url = Server.MapPath("/img/" + fileName);
                    //保存文件
                    ful.SaveAs(url);
                    //设置图片控件显示目录
                    Image1.ImageUrl = "/img/" + fileName;
                    //设置图片为 显式的  显示在页面上
                    Image1.Visible = true;
                }
            }
        }
    }
}